package org.NooLab.itexx.browser;

import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Iterator;

import chrriis.dj.nativeswing.swtimpl.NativeComponent;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;



/**
 * 
 * 
 * 
 *
 */
public class BrowserEventHandler {

	DjBrowserListener parentListener ; 
	JWebBrowser webBrowser ;
	NativeComponent nativeComponent;
	private int  lastCanvasKey;
	private long lastCanvaslKeyTime;
	
	// ========================================================================
	public BrowserEventHandler(DjBrowserListener browserlistener, JWebBrowser webbrowser) {

		parentListener = browserlistener; 
		webBrowser = webbrowser;
	}
	// ========================================================================

	public void close() {
		NativeComponent c = nativeComponent;

		KeyListener[] kls;
		MouseListener[] mls;

		// we need cls only in case of popup ? ... cls =
		// c.getComponentListeners();
		mls = c.getMouseListeners();
		kls = c.getKeyListeners();

		for (int i = 0; i < mls.length; i++) {
			c.removeMouseListener(mls[i]);
		}
		for (int i = 0; i < kls.length; i++) {
			c.removeKeyListener(kls[i]);
		}

	}

	public void define() {
	
		
		/* for popup menu... 
		nativeComponent = webBrowser.getNativeComponent();   
	    // Add the popup menu   
	    webBrowser.setDefaultPopupMenuRegistered(false);   
	    nativeComponent.addMouseListener(new MouseAdapter() {   
	      @Override  
	      public void mousePressed(MouseEvent e) {   
	        maybeShowPopup(e);   
	      }   
	      @Override  
	      public void mouseReleased(MouseEvent e) {   
	        maybeShowPopup(e);   
	      }   
	      /*
	      private void maybeShowPopup(MouseEvent e) {   
	        if(e.isPopupTrigger()) {   
	          JPopupMenu popupMenu = new JPopupMenu();   
	          popupMenu.add(new JMenuItem("A Swing menu item in a Swing menu!"));   
	          popupMenu.add(new JMenuItem("Another Swing menu item!"));   
	          popupMenu.addSeparator();   
	          popupMenu.add(new JMenuItem("Yet another one!"));   
	          popupMenu.show(e.getComponent(), e.getX(), e.getY());   
	        }   
	      } 
	        
	    });  
	    */
		nativeComponent = webBrowser.getNativeComponent();
		nativeComponent.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// textArea.append(e.toString() + "\n");
				webBrowserMouseClicked(e);
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// textArea.append(e.toString() + "\n");
			}
		});
		nativeComponent.addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent e) {
				// textArea.append(e.toString() + "\n");
				
			}
		});

		nativeComponent.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				panelsTextPaneKeyPressed( e.getKeyCode(), e);
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// textArea.append(e.toString() + "\n");
			}
		});

	}

	public void webBrowserMouseClicked(MouseEvent evt) {
		// 
		String lastlink = parentListener.getLastLink();
		parentListener.out.print(2, "web browser MouseClicked , event = "+evt) ;
		parentListener.out.print(2, "            last known link      : "+lastlink) ;
	}
	
	/*
	 
	  browser
      	- ctrl-y  =  repeat, ctrl-r = back ctrl-f forward 
      	- ctrl-a  == adopt the page in the browser into iTexx == just reloading
      	- ctrl-s  == take the page as a basis for next search 

	 */
	// lastCanvasKey = 0; lastCanvaslKeyTime = -1L;
	private void panelsTextPaneKeyPressed( int keycode, KeyEvent evt) {
		// 
		boolean keymatch=false;
		
		parentListener.out.print(2, "canvas key pressed : "+ keycode + " ("+evt.getKeyCode()+"), "+evt.toString());
		
		if ((lastCanvaslKeyTime>1000000) && (lastCanvaslKeyTime-System.currentTimeMillis()<18)){
		
			if ((new KeyCodes(evt.getKeyCode(),lastCanvasKey)).is("ctrl","r")){
				// previous page = rewind
				parentListener.out.printErr(2,"  -> GoTo Previous Page!");
				keymatch = true;
			}
			if ((new KeyCodes(evt.getKeyCode(),lastCanvasKey)).is("ctrl","f")){
				// next page = forward
				parentListener.out.printErr(2,"  -> GoTo Next Page as a deja vu!");
				keymatch = true;
			}
			if ((new KeyCodes(evt.getKeyCode(),lastCanvasKey)).is("ctrl","a")){
				// adopt page to itexx
				parentListener.out.printErr(2,"  -> Adopt current Page to iTexx (as a positive feedback)");
				parentListener.out.printErr(2,"  ->       address of current page is : ...");
				keymatch = true;
			}
			if ((new KeyCodes(evt.getKeyCode(),lastCanvasKey)).is("ctrl","y")){
				// reload
				parentListener.out.printErr(2,"  -> Reload Page!");
				keymatch = true;
			}
			if ((new KeyCodes(evt.getKeyCode(),lastCanvasKey)).is("ctrl","s")){
				// derive next search
				parentListener.out.printErr(2,"  -> Derive next search from current page!");
				keymatch = true;
			}



			
			if (keymatch){
				lastCanvasKey = 0;
				lastCanvaslKeyTime = -1L;

			}
		}else{
			lastCanvasKey = evt.getKeyCode() ;
			lastCanvaslKeyTime = System.currentTimeMillis();
		}
	}
}
